<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of carpoolAction
 *
 * @author xlp
 */
class carpoolAction extends commonAction {

    private $cacheFilePre = 'carpool/visit_vid_'; //保存缓存信息的文件名前缀
    private $info = array('否', '<span class="red">是</span>');
    private $status = array('<span class="red">屏蔽</span>', '正常');
    private $type = array('找车主', '找乘客');

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    function index() {
        $title = $this->_get('q');
        $stime = $this->_get('stime');
        $etime = $this->_get('etime');
        $p = $this->_getid('p', 1);
        $uid = $this->_getid('uid', 0);
        $village_id = $this->_getid('village_id', 0);
        $type = $this->_getid('type', 0);
        $where = array();
        if ($title) {
//            $where['LIKE'] = array('title' => parent::safeSearch($title));
        }
        if ($stime && $etime) {
            $stime = inTime($stime);
            $etime = inTime($etime) + (24 * 60 * 60) - 1;
        }
        if ($stime && $etime) {
            $where['infotime[>=]'] = $stime;
            $where['infotime[<=]'] = $etime;
        }
        if ($uid) {
            $where = array('uid' => $uid);
        }
        if ($village_id) {
            $where = array('village_id' => $village_id);
        }
        if ($type) {
            $where = array('type' => ($type - 1));
        }
        $rs = M('carpool')->where($where)->page($p)->order('cid DESC')->findAll();
        $total = M('carpool')->getTotal();
//        z(M('area')->getAllSql());
        if ($rs) {
            foreach ($rs as $k => $v) {
                $rs[$k]['user_name'] = getUser($v['uid']);
                $rs[$k]['village_name'] = parent::getVillageName($v['village_id']);
            }
        }
        $this->assign(array('rs' => $rs, 'total' => $total, 'p' => $p, 'info' => $this->info, 'status' => $this->status, 'type' => $this->type));
//        print_r($this->getAllVar());
        $this->display();
    }

    function detail() {
        $cid = $this->_getid('cid');
        if (!$cid) {
            showError('参数丢失');
        }
        $rs = M('carpool')->where(array('cid' => $cid))->find();
        if (!$rs) {
            showError('主题不存在或者已被删除');
        }
        $rs['lbs_info'] = json_decode($rs['lbs_info'], true);
//        z($rs);
        $this->assign(array('rs' => $rs));
        $this->display();
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        //检查主题是否存在
        $carpool = M('carpool')->field('uid,village_id')->where(array('cid' => $id))->find();
        if (!$carpool) {
            $this->JsonReturn('主题不存在，请检查');
        }
        M('carpool')->update(array('status' => 0), array('cid' => $id));
//        //删除所有回帖
//        M('carpool_post')->delete(array('fid' => $id));
//        //删除点赞
//        M('carpool_praise')->delete(array('fid' => $id));
        F($this->cacheFilePre . $carpool['village_id'], null); //清除统计缓存
        $this->JsonReturn('ok', null, 1);
    }

}
